package org.game.module.datingbo;

import com.sojoys.artifact.constant.ArtifactErrorCode;
import com.sojoys.artifact.exception.AbstractLogicModelException;
import com.sojoys.artifact.factory.session.ISession;
import com.sojoys.artifact.manager.MessageManager;
import com.sojoys.artifact.tools.ToolError;
import com.sojoys.artifact.tools.ToolMap;
import com.xiaoleilu.hutool.log.Log;
import com.xiaoleilu.hutool.log.LogFactory;
import org.cfg.DatingRuleCfg;
import org.constant.DatingConstant;
import org.error.GameErrorCode;
import org.game.constant.DatingTypeConstant;
import org.game.constant.SessionConstent;
import org.game.db.bean.CityDatingRecord;
import org.game.db.bean.CurrentScriptRecord;
import org.game.db.bean.Player;
import org.game.db.bean.Role;
import org.game.db.cache.CityDatingRecordCache;
import org.game.db.cache.CurrentScriptRecordCache;
import org.game.db.cache.RoleCache;
import org.game.db.proxy.bean.PlayerProxy;
import org.game.log.LogDating;
import org.game.log.builder.LogDsp;
import org.game.log.dsp.DatingDsp;
import org.game.log.dsp.GoodsDsp;
import org.game.manager.DatingManager;
import org.game.manager.ItemManager;
import org.game.manager.RoleManager;
import org.game.module.datingbo.handler.ReserveDatingHandler;
import org.game.module.rolebo.RoleBO;
import org.game.msgBuilder.DatingMsgBuilder;
import org.game.msgBuilder.RoleMsgBuilder;
import org.game.protobuf.c2s.C2SDatingMsg;
import org.game.protobuf.s2c.S2CDatingMsg;
import org.game.protobuf.s2c.S2CShareMsg;
import org.game.util.ProtoUnit;

import java.util.Map;

/**
 * 约会
 * Created by fxf on 2017/8/15.
 */
public class DatingBOImpl implements DatingBO {

    static Log log = LogFactory.get();

    /**
     * 获取剧本 [code = 1537]
     */
    @Override
    public byte[] getScript(ISession session, C2SDatingMsg.GetScriptMsg msg) throws AbstractLogicModelException {

        Player player = session.getAttr(SessionConstent.PLAYER);
        int scriptType = msg.getScriptType().getNumber();

        int roleId = msg.getRoleId();
        Role role = RoleCache.me().getByCidPlayerId(roleId,player.getId());
        ToolError.isAndTrue(GameErrorCode.CLIENT_PARAM_IS_ERR,"客户端 roleId 错误" ,role == null);

        CurrentScriptRecord record = CurrentScriptRecordCache.me().getCurrentScriptRecordCacheProxy().getByPlayerIdDatingTypeRoleId(player.getId(), scriptType, roleId);
        ToolError.isAndTrue(GameErrorCode.HAVE_NOT_FINISH_SCRIPT, record != null && record.getScriptId() != 0);

        DatingManager.getScript(player, scriptType, msg);
        return new byte[]{};
    }

    /**
     * 约会对话 [code = 1538]
     */
    @Override
    public byte[] datingDialogue(ISession session, C2SDatingMsg.DialogueMsg msg) throws AbstractLogicModelException {
        Player player = session.getAttr(SessionConstent.PLAYER);
        return DatingManager.dialog(player, msg);
    }

    /**
     * 约会信息 [code = 1539]
     */
    @Override
    public byte[] getDatingInfo(ISession session) throws AbstractLogicModelException {
        Player player = session.getAttr(SessionConstent.PLAYER);
        S2CDatingMsg.GetDatingInfo datingInfo = DatingMsgBuilder.getDatingInfoMsg(player);
        return ProtoUnit.toByte(datingInfo);
    }

    /**
     * 回应约会邀请
     */
    @Override
    public byte[] answerDatingInvitation(ISession session, C2SDatingMsg.AnswerDatingInvitationMsg msg) throws AbstractLogicModelException {
        Player player = session.getAttr(SessionConstent.PLAYER);
        long datingId = Long.valueOf(msg.getDatingId());
        int answer = msg.getAnswer();

        CityDatingRecord record = CityDatingRecordCache.me().getById(datingId);
        ToolError.isAndTrue(ArtifactErrorCode.PARAM_ERROR, "没有找到约会记录", record == null);
        ToolError.isAndTrue(ArtifactErrorCode.PARAM_ERROR, "并非受邀请状态", record.getCityDatingRecordProxy().getState()  != DatingConstant.RESERVE_DATING_STATE_HAVE_INVITATION);

        DatingRuleCfg cfg = record.getCityDatingRecordProxy().getCfg();
        Map answerInfo = (Map) ((Map) cfg.getOtherInfo().get(DatingConstant.OTHER_INFO_KEY_ANSWER_MAP)).get(answer);

        boolean isAccept = ((int)answerInfo.get(DatingConstant.ANSWER_TYPE)) == 1;
        
        PlayerProxy playerExt = player.getPlayerProxy();

        ReserveDatingHandler handler = (ReserveDatingHandler) DatingManager.getDatingHandlerByType(DatingTypeConstant.DATING_TYPE_RESERVE);
        handler.answerInvitation(player,record,isAccept);

        Role role = RoleCache.me().getByCidPlayerId(record.getCityDatingRecordProxy().getRoleCids().get(0),player.getId());

        LogDsp logDsp = LogDsp.newBuilder(GoodsDsp.RESERVE_ANSWER);
        int favor = ToolMap.getInt(DatingConstant.ANSWER_FAVOR,answerInfo,0);
        if (favor != 0){
            RoleManager.me().changeFavor(role,favor,logDsp);
        }
        int mood = ToolMap.getInt(DatingConstant.ANSWER_MOOD,answerInfo,0);
        if(mood != 0){
            RoleManager.me().changeMood(role,mood,logDsp);
        }

        playerExt.sendMsg(MessageManager.me().create(RoleBO.updateRoleMood, ProtoUnit.toByte(RoleMsgBuilder.createRoleInfo(S2CShareMsg.ChangeType.UPDATE, role))));
        LogDating.me().log(player.getId(), DatingTypeConstant.DATING_TYPE_RESERVE,0,role.getCid(), LogDsp.newBuilder(DatingDsp.RESPONSE_INVITE).addAttr("ReserveCfgId",cfg.getId()).addAttr("answerType",answer));

        playerExt.sendMsg(DatingBO.sendCityDating,DatingMsgBuilder.getCityDatingInfo(isAccept? S2CShareMsg.ChangeType.UPDATE: S2CShareMsg.ChangeType.DELETE,record));
        return new byte[0];
    }
}
